/*
 * Copyright © 2012 Avego Ltd., All Rights Reserved.
 * For licensing terms please contact Avego LTD.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.clkj.tramcarlibrary.service;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.clkj.tramcarlibrary.R;
import com.clkj.tramcarlibrary.constant.Constants;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class NetworkSetting {

	private final static String AVEGO_URL = "http://www.pandabus.cn/";

	/**
	 * This check network is usable
	 * @param inContext
	 * @return boolean
	 */
	public static boolean isNetworkActive(Context inContext) {
		ConnectivityManager cm = (ConnectivityManager) inContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting()) {
			return true;
		}
		return false;
	}

	/**
	 * This check network by url
	 * @return boolean
	 */
	public static boolean checkNetworkByUrl() {
		URL url;
		try {
			url = new URL(AVEGO_URL);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			int state = connection.getResponseCode();
			String responseContent = connection.getResponseMessage();
			if (state == 200 && "OK".equals(responseContent)) {
				return true;
			}

		} catch (MalformedURLException e) {
			Log.e(Constants.DEBUG_TAG, "MalformedURLException:" + e.getMessage());
		} catch (IOException e) {
			Log.e(Constants.DEBUG_TAG, "IOException" + e.getMessage());
		}
		return false;

	}
	
	private static AlertDialog dialog;
	public static void openSetting(final Activity act) {
		dialog = new AlertDialog.Builder(act).setTitle(act.getString(R.string.whoops)).setMessage(act.getString(R.string.network_warn))
				.setPositiveButton(act.getString(R.string.setting), new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						act.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));

					}
				}).setNegativeButton(act.getString(R.string.exit), new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (!act.isFinishing()) {
							dialog.dismiss();
						}
						act.finish();

					}
				}).create();
		dialog.setCancelable(false);
		dialog.show();
	}
	
	public static void checkNeworkOver() {
		if (NetworkSetting.dialog != null && NetworkSetting.dialog.isShowing()) {
			NetworkSetting.dialog.dismiss();
		}
	}

}
